﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dapper;
using nanoCMS.Core;
using nanoCMS.Store.Concrete;
using nanoCMS.Core.Extensions;


namespace nanoCMS.Store.Providers
{
    public class StoreCatalogItemProvider
    {
        private readonly WorldContext worldContext;
        private StoreEntities entities;

        private StoreEntities Entities
        {
            get { return entities ?? (entities = new StoreEntities(worldContext.EntityConnectionString)); }
        }

        public StoreCatalogItemProvider(WorldContext worldContext)
        {
            this.worldContext = worldContext;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="title"></param>
        /// <param name="description"></param>
        /// <param name="itemCode"></param>
        /// <param name="isAvailable"></param>
        /// <param name="tenantID"></param>
        /// <returns>New StoreCatalogItem ID</returns>
        public int Add(string title, string description, string itemCode, bool isAvailable)
        {
            StoreCatalogItem model = new StoreCatalogItem
                                         {
                                             Title = title,
                                             Description = description,
                                             ItemCode = itemCode,
                                             IsAvailable = isAvailable,
                                             StoreID = 1//TODO: storeID 
                                         };

            Entities.StoreCatalogItems.AddObject(model);
            Entities.SaveChanges();
            return model.StoreCatalogItemID;
        }

        public bool Edit(int id, string title, string description, string itemCode, bool isAvailable)
        {
            StoreCatalogItem model = Entities.StoreCatalogItems.SingleOrDefault(x => x.StoreCatalogItemID == id);
            if (model == null) { return false; }

            model.Title = title;
            model.Description = description;
            model.ItemCode = itemCode;
            model.IsAvailable = isAvailable;

            Entities.SaveChanges();
            return true;
        }

        public StoreCatalogItem GetByCode(string itemCode, int storeId)
        {
            var data = Entities.StoreCatalogItems.SingleOrDefault(x => x.ItemCode == itemCode && x.StoreID == storeId);
            if (data == null) throw new ArgumentException("There isn't StoreCatalogItem with itemCode: " + itemCode);
            return data;
        }

        public StoreCatalogItem Get(int itemId)
        {
            var data = Entities.StoreCatalogItems.SingleOrDefault(x => x.StoreCatalogItemID == itemId);
            if (data == null) throw new ArgumentException("There isn't StoreCatalogItem with provided itemID");
            return data;
        }
    }
}
